using System;
using System.Collections.Generic;
using System.Text;

namespace Eve.Data
{
    public class AssemblyLineType : EveProperty
    {
        private string assemblyLineTypeName;
        private string description;
        private double baseTimeMultiplier;
        private double baseMaterialMultiplier;
        private double volume;
        private int activityId;
        private double minCostPerHour;

        private RAMActivity activity;
        private List<AssemblyLineStation> stations;
        private Dictionary<Category, AssemblyLineTypeCategoryDetail> categoryDetail;
        private Dictionary<Group, AssemblyLineTypeGroupDetail> groupDetail;

        public string Name
        {
            get { return assemblyLineTypeName; }
        }
        public string Description
        {
            get { return description; }
        }
        public double BaseTimeMultiplier
        {
            get { return baseTimeMultiplier; }
        }
        public double BaseMaterialMultiplier
        {
            get { return baseMaterialMultiplier; }
        }
        public double Volume
        {
            get { return volume; }
        }
        public RAMActivity Activity
        {
            get
            {
                if (activity == null)
                    activity = new RAMActivity(db, activityId);
                return activity;
            }
        }
        public double MinimumCostPerHour
        {
            get { return minCostPerHour; }
        }
        public List<AssemblyLineStation> Stations
        {
            get
            {
                if (stations == null)
                {
                    stations = new List<AssemblyLineStation>();
                    string query = "SELECT * FROM ramAssemblyLineStations WHERE assemblyLineTypeID=" + Id;
                    foreach (DbDataRow row in db.ExecuteQuery(query))
                        stations.Add(new AssemblyLineStation(row, this));
                }
                return stations;
            }
        }

        public AssemblyLineTypeCategoryDetail GetCategoryDetails(Category category)
        {
            if (categoryDetail == null)
            {
                categoryDetail = new Dictionary<Category, AssemblyLineTypeCategoryDetail>();
                string query = "SELECT * FROM ramAssemblyLineTypeDetailPerCategory WHERE assemblyLineTypeID=" + Id;
                foreach (DbDataRow row in db.ExecuteQuery(query))
                {
                    AssemblyLineTypeCategoryDetail altcd = new AssemblyLineTypeCategoryDetail(row, this);
                    categoryDetail[altcd.Category] = altcd;
                }
            }
            if (categoryDetail.ContainsKey(category)) return categoryDetail[category];
            return null;
        }
        public AssemblyLineTypeGroupDetail GetGroupDetails(Group group)
        {
            if (groupDetail == null)
            {
                groupDetail = new Dictionary<Group, AssemblyLineTypeGroupDetail>();
                string query = "SELECT * FROM ramAssemblyLineTypeDetailPerGroup WHERE assemblyLineTypeID=" + Id;
                foreach (DbDataRow row in db.ExecuteQuery(query))
                {
                    AssemblyLineTypeGroupDetail altgd = new AssemblyLineTypeGroupDetail(row, this);
                    groupDetail[altgd.Group] = altgd;
                }
            }
            if (groupDetail.ContainsKey(group)) return groupDetail[group];
            return null;
        }

        internal AssemblyLineType(DbDataRow dr)
            : base(dr.Database, EvePropertyType.AssemblyLineType, dr["assemblyLineTypeID"])
        {
            Fill(dr);
        }
        internal AssemblyLineType(Database db, int id)
            : base(db, EvePropertyType.AssemblyLineType, id)
        {
            Fill(db.ExecuteSingle("SELECT * FROM ramAssemblyLineTypes WHERE assemblyLineTypeID=" + id));
        }

        private void Fill(DbDataRow dr)
        {
            assemblyLineTypeName = dr["assemblyLineTypeName"];
            description = dr["description"];
            baseTimeMultiplier = dr["baseTimeMultiplier"];
            baseMaterialMultiplier = dr["baseMaterialMultiplier"];
            volume = dr["volume"];
            activityId = dr["activityID"];
            minCostPerHour = dr["minCostPerHour"];
        }
    }
}
